package com.innovation.chengzhangriji.ui.widget;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public abstract class DiscoverCommentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public interface OnClickDiscoverCommentListener {

        void onClickLayout(DiscoverCommentViewHolder holder);

        void onClickHead(DiscoverCommentViewHolder holder);

    }

    private OnClickDiscoverCommentListener onClickDiscoverCommentListener;

    private RoundedImageView imageHead;

    private TextView textName;

    private TextView textComment;

    private ImageView imageLike;

    private TextView textTime;

    private TextView textFrom;

    private FrameLayout content;

    public DiscoverCommentViewHolder(View itemView) {
        super(itemView);
        RelativeLayout layout = (RelativeLayout) itemView.findViewById(R.id.layout);
        layout.setOnClickListener(this);
        imageHead = (RoundedImageView) itemView.findViewById(R.id.image_head);
        textName = (TextView) itemView.findViewById(R.id.text_name);
        textComment = (TextView) itemView.findViewById(R.id.text_comment);
        imageLike = (ImageView) itemView.findViewById(R.id.image_like);
        textTime = (TextView) itemView.findViewById(R.id.text_time);
        textFrom = (TextView) itemView.findViewById(R.id.text_from);
        content = (FrameLayout) itemView.findViewById(R.id.content);
    }

    @Override
    public void onClick(View v) {
        if (onClickDiscoverCommentListener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.layout:
                onClickDiscoverCommentListener.onClickLayout(this);
                break;
            case R.id.image_head:
                onClickDiscoverCommentListener.onClickHead(this);
                break;
        }
    }

    protected final FrameLayout getContent() {
        return content;
    }

    public void setOnClickDiscoverCommentListener(OnClickDiscoverCommentListener onClickDiscoverCommentListener) {
        this.onClickDiscoverCommentListener = onClickDiscoverCommentListener;
    }

    public void setHeadUrl(String url) {
        ImageLoader.getInstance().displayImage(url, imageHead, Config.getOptionsHead());
    }

    public void setName(String name) {
        textName.setText(name);
    }

    public void setComment(String comment) {
        if (TextUtils.isEmpty(comment)) {
            textComment.setVisibility(View.INVISIBLE);
        } else {
            textComment.setVisibility(View.VISIBLE);
            textComment.setText(comment);
        }
    }

    public void setLike(boolean like) {
        if (like) {
            imageLike.setVisibility(View.VISIBLE);
        } else {
            imageLike.setVisibility(View.INVISIBLE);
        }
    }

    public void setTime(String time) {
        textTime.setText(time);
    }

    public void setFrom(String from) {
        textFrom.setText(from);
    }

}
